package com.netease.financial.module.browser;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.netease.financial.common.constant.Constants;
import com.netease.financial.common.util.preferences.BadgePreferencesHelper;
import com.netease.financial.internal.di.components.BrowserComponent;
import com.netease.financial.module.web.WebViewTabFragment;

import javax.inject.Inject;

import cn.bingoogolapple.badgeview.BGABadgeable;

public class BrowserFragment extends WebViewTabFragment implements BrowserView {

    private static final String TAG = BrowserFragment.class.getSimpleName();
    private BrowserFragment self;

    @Inject
    BrowserPresenter browserPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = this;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize();
        loadAsset();
        handleArguments();
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleArguments();

    }

    private void initialize() {
        getComponent(BrowserComponent.class).inject(this);
        browserPresenter.setView(this);
    }

    private void handleArguments() {
        String url = this.getArguments().getString(MainBrowserActivity.EXTRA_BROWSER_URL);
        this.getWebView().loadUrl(url.substring(url.indexOf("fc://") + 5));
        this.getWebView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (self.getWebView().canGoBack()) {
                    self.getWebView().goBack();
                    return true;
                }
                return false;
            }
        });

    }


    /**
     * Loads .
     */
    private void loadAsset() {
        browserPresenter.initialize();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public String getInitialPageUrl() {
        return Constants.getAssetUrl();
    }

    @Override
    public boolean isTabBadgeShouldShow() {
        return BadgePreferencesHelper.isAssetTabBadgeShouldShow();
    }

    @Override
    public void showBadge(BGABadgeable badgeView) {
        int unreadMessageCount = BadgePreferencesHelper.getUnreadPrivateMessageCount();
        if (unreadMessageCount > 0) {
            badgeView.showTextBadge(String.valueOf(unreadMessageCount));
        } else {
            badgeView.showCirclePointBadge();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {

    }
}
